package com.yisu.auction.manager.website.factoryClassPackage.excelFactory.impl;

import com.yisu.auction.manager.domain.vo.baseInfoPackageVos.MemberVo;
import com.yisu.auction.manager.website.factoryClassPackage.excelFactory.IExportService;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * author: 赵杰
 * Business Description：
 * createTime：2017/9/27
 */
public class memberDataExportImpl implements IExportService {
    /**
     * author: 赵杰
     * Business Description：导出业务
     *
     * @param filePath
     * @param data
     */
    @Override
    public void export(String filePath, Object data) {
        OutputStream out = null;
        List<MemberVo> memberVos = (List<MemberVo>) data;
        try {
            out = new FileOutputStream(new File(filePath));
            reportExport(out, memberVos);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    protected void reportExport(OutputStream outputStream, List<MemberVo> memberVos) {
        // 初始一个workbook
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 创建单个sheet
        HSSFSheet sheet = workbook.createSheet("sheet0");
        // 创建第一行，设置列名
        HSSFRow row0 = sheet.createRow(0);
        row0.setHeight((short) 400);             // 设置行高
        HSSFCell cell = null;                    // Excel的列
        HSSFFont font = workbook.createFont();          // 设置字体
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);   // 粗体显示
        font.setColor(HSSFFont.COLOR_NORMAL);           // 设置单元格字体的颜色
        HSSFCellStyle style = workbook.createCellStyle();   // 样式
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        // 设置边框样式
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        // 设置边框颜色
        style.setTopBorderColor(HSSFColor.BLACK.index);
        style.setBottomBorderColor(HSSFColor.BLACK.index);
        style.setLeftBorderColor(HSSFColor.BLACK.index);
        style.setRightBorderColor(HSSFColor.BLACK.index);
        // 设置文本格式
        HSSFDataFormat format = workbook.createDataFormat();
        style.setDataFormat(format.getFormat("@"));
        StringBuffer tableHeaderStr = new StringBuffer("用户ID;创建时间;昵称;手机;余额;赠送余额;消费金额;累计充值金额;用户地址;注册渠道");
        String[] tableHeader = tableHeaderStr.toString().split(";");
        short cellNumber = (short) tableHeader.length;  // 表的列数
        for (int k = 0; k < cellNumber; k++) {
            int i = 0;
            i += k;
            short b = 5000;
            cell = row0.createCell(i);          // 创建第0行第k列
            cell.setCellValue(tableHeader[k]);  // 设置第0行第k列的值
            sheet.setColumnWidth(i, b);         // 设置列的宽度
            style.setFont(font);                // 设置字体风格
            cell.setCellStyle(style);
        }
        HSSFCellStyle styleContext = workbook.createCellStyle();
        styleContext.setWrapText(true);
        styleContext.setAlignment(HSSFCellStyle.ALIGN_LEFT);
        // 设置边框样式
        styleContext.setBorderTop(HSSFCellStyle.BORDER_THIN);
        styleContext.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        styleContext.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        styleContext.setBorderRight(HSSFCellStyle.BORDER_THIN);
        // 设置边框颜色
        styleContext.setTopBorderColor(HSSFColor.BLACK.index);
        styleContext.setBottomBorderColor(HSSFColor.BLACK.index);
        styleContext.setLeftBorderColor(HSSFColor.BLACK.index);
        styleContext.setRightBorderColor(HSSFColor.BLACK.index);
        // 设置文本格式
        styleContext.setDataFormat(format.getFormat("@"));

        if (memberVos.size() > 0) {
            // 创建剩余行
            for (int rowIndex = 1; rowIndex <= memberVos.size(); rowIndex++) {
                HSSFRow row = sheet.createRow(rowIndex);
                MemberVo entity = memberVos.get(rowIndex - 1);
                // 创建多列
                for (int cellIndex = 0; cellIndex < 10; cellIndex++) {
                    cell = row.createCell(cellIndex);
                    cell.setCellStyle(styleContext);               // 设置风格
                    switch (cellIndex) {
                        case 0://用户ID;
                            cell.setCellValue(entity.getId());
                            break;
                        case 1://创建时间;
                            SimpleDateFormat sformatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                            cell.setCellValue(sformatDate.format(entity.getCreateDate()));
                            break;
                        case 2://昵称;
                            cell.setCellValue(entity.getNickname());
                            break;
                        case 3://手机;
                            cell.setCellValue(entity.getMobile());
                            break;
                        case 4://余额;
                            cell.setCellValue(String.valueOf(0));
                            if (entity.getWallet() != null)
                                cell.setCellValue(String.valueOf(entity.getWallet()));
                            break;
                        case 5://赠送余额;
                            cell.setCellValue(String.valueOf(0));
                            if (entity.getPresentWallet() != null)
                                cell.setCellValue(String.valueOf(entity.getPresentWallet()));
                            break;
                        case 6://消费金额;
                            cell.setCellValue(String.valueOf(0));
                            if (entity.getConsumptionAmount() != null)
                                cell.setCellValue(String.valueOf(entity.getConsumptionAmount()));
                            break;
                        case 7://累计充值金额;
                            cell.setCellValue(String.valueOf(0));
                            if (entity.getRechargeMoney() != null)
                                cell.setCellValue(String.valueOf(entity.getRechargeMoney()));
                            break;
                        case 8://用户地址;注册渠道
                            cell.setCellValue(entity.getAddress());
                            break;
                        case 9://注册渠道
                            cell.setCellValue(entity.getPackageName());
                            break;
                    }
                }
            }
        } else {
            HSSFRow row = sheet.createRow(1);
            cell = row.createCell(0);
            cell.setCellValue("没有明细");
        }
        try {
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
